﻿using Panuon.UI.Silver;
using Panuon.UI.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using WaveringLand_Launcher.Helper;
using WaveringLand_Launcher.Helper.Common;
using WaveringLand_Launcher.Model;
using WaveringLand_Launcher.ViewModel.Component;

namespace WaveringLand_Launcher.ViewModel
{
    [PropertyChanged.AddINotifyPropertyChangedInterface]
    public class GameResourcesViewModel
    {

        public static GameResourcesViewModel Instance { get; set; }


        private ObservableCollection<ResFileViewModel> Index { get; set; }


        public bool ModNeedUpdate => MustModsIndex.ToList().Exists(m => m.NeedUpdate) || ServersDatIndex.ToList().Exists(m => m.NeedUpdate) || hasExcess;

        public ObservableCollection<ResFileViewModel> MustModsIndex => new(Index.Where(s => s.WllFile.Category is "Mods.Must" or "Mods"));
        public ObservableCollection<ResFileViewModel> ExtraModsIndex => new(Index.Where(s => s.WllFile.Category == "Mods.Extra"));
        public ObservableCollection<ResFileViewModel> ServersDatIndex => new(Index.Where(s => s.WllFile.Category == "ServersDat"));


        public ObservableCollection<ResFileViewModel> PacksIndex => new(Index.Where(s => s.WllFile.Category is "Shaderpacks" or "Resourcepacks" or "Extra"));

        public ObservableCollection<ResFileViewModel> LauncherResIndex => new(Index.Where(s => s.WllFile.Category is "LauncherRes"));
        public bool isWaiting { get; set; } = false;
        public bool hasExcess { get; set; } = false;
        public int SelectedIndex { get; set; } = 0;

        public ICommand ChangedTabCommand { get; set; }
        public ICommand GetLatestBtnClickCommand { get; set; }
        public ICommand LoadedCommand { get; set; }
        public ICommand DeleteExcessModsCommand { get; set; }
        public ICommand DownloadAllMustCommand { get; set; }

        public GameResourcesViewModel()
        {
            Instance = this;
            Index = new();
            ChangedTabCommand = new RelayCommand(ChangedTab);
            GetLatestBtnClickCommand = new RelayCommand(GetLatest);
            LoadedCommand = new RelayCommand(Loaded);
            DeleteExcessModsCommand = new RelayCommand(DeleteExcessMods);
            DownloadAllMustCommand = new RelayCommand(DownloadAllMust);
        }
        void ChangedTab(object sender)
        {
            SelectedIndex = int.Parse((sender as RadioButton).Tag.ToString());
        }
        /// <summary>
        /// 就是获取数据并刷新Index来刷新页面
        /// </summary>
        /// <param name="sender"></param>
        async void GetLatest(object sender)
        {
            if (isWaiting)
                return;

            if (Index.ToList().Exists(x => x.IsDownloading))
                return;


            isWaiting = true;
            List<WllFile> index = new(await WlTaskHelper.GetLatestIndexAsync());
            Index.Clear();
            foreach (WllFile item in index)
            {
                Index.Add(new() { WllFile = item, NeedUpdate = item.CheckNeedUpdate() });
            }
            Index = new ObservableCollection<ResFileViewModel>(Index);

            //check 本地多余mods
            hasExcess = (await ResHelper.GetExcessMods()).Count != 0;

            isWaiting = false;
        }
        void Loaded(object sender)
        {
            GetLatest(sender);
        }

        async void DeleteExcessMods(object sender)
        {
            var list = await ResHelper.GetExcessMods();
            if(list is { Count:0})
            {
                Debug.WriteLine("no excess");
                return;
            }
                App.Current.Dispatcher.Invoke(() => {
                MessageBoxX.Show("以下文件会被删除:\n" + string.Join('\n', list.Select(s=>Path.GetFileName(s))));
            foreach (var item in list)
            {
                File.Delete(item);
            }
                MessageBoxX.Show("删除完成");

            });
            GetLatest(sender);
        }

        void DownloadAllMust(object sender)
        {
            foreach (var item in MustModsIndex.Where(x=>x.NeedUpdate))
            {
                item.DownloadResCommand.Execute(sender);
            }
            foreach (var item in ServersDatIndex.Where(x => x.NeedUpdate))
            {
                item.DownloadResCommand.Execute(sender);
            }
        }
    }
}
